{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import DataSetQuery\n",
    "import MalardHelpers\n",
    "import datetime\n",
    "\n",
    "#Sets the URL of the Malard ServiceGateway.\n",
    "query = DataSetQuery.DataSetQuery('http://localhost:9000','DEV')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Setup the bounding box\n",
    "import pandas as pd\n",
    "\n",
    "minX=0\n",
    "maxX=100000\n",
    "minY=0\n",
    "maxY=100000\n",
    "minT=datetime.datetime(2012,1,1,0,0)\n",
    "maxT=datetime.datetime(2012,1,31,0,0)\n",
    "\n",
    "#a list of columns to see in the output (x,y,time will be added)\n",
    "projections = ['lat','lon','elev','power','coh','swathFileId'] \n",
    "#a list of dictionaries that specify the filters\n",
    "#only numeric fields are supported with the following operations gt(greater than), gte ( greater than equals), lt (less than) and lte (less than equals)\n",
    "#filters are treated as and conditions\n",
    "filters = [{'column':'swathFileId','op':'gte','threshold':11783}]\n",
    "\n",
    "#No filters\n",
    "nofiltersTandemX = query.executeQuery( 'mtngla','tandemx',minX,maxX,minY,maxY,minT,maxT,projections,filters)\n",
    "print(nofiltersTandemX)\n",
    "dfTx = MalardHelpers.getDataFrameFromNetCDF(nofiltersTandemX)\n",
    "\n",
    "nofiltersSrtm = query.executeQuery( 'mtngla','srtm',minX,maxX,minY,maxY,minT,maxT,projections,filters)\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "dfSrtm = MalardHelpers.getDataFrameFromNetCDF(nofiltersSrtm)\n",
    "\n",
    "print(\"TandemX MaxElev:[%f] MinElev:[%f] Points:[%d]\" % (dfTx['elev'].max(),dfTx['elev'].min(),dfTx['elev'].count()))\n",
    "print(\"Srtm MaxElev:[%f] MinElev:[%f] Points:[%d]\" % (dfSrtm['elev'].max(),dfSrtm['elev'].min(),dfSrtm['elev'].count()))\n",
    "\n",
    "\n",
    "df = pd.merge(dfTx,dfSrtm,left_on=['lat','lon'],right_on=['lat','lon'],how='left')\n",
    "\n",
    "df.to_csv(\"/data/puma1/scratch/test/alljoin.csv\")\n",
    "\n",
    "query.releaseCache(nofiltersTandemX)\n",
    "query.releaseCache(nofiltersSrtm)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(dfTx.groupby(['swathFileId']).agg(['count']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(dfSrtm.groupby(['swathFileId']).agg(['count']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "sourceTandemX = '/data/puma1/scratch/test/tandemx/CS_LTA__SIR_SIN_2S_20120122T040316_20120122T040631_C001.nc'\n",
    "sourceSrtm = '/data/puma1/scratch/test/srtm/CS_LTA__SIR_SIN_2S_20120122T040316_20120122T040631_C001.nc'\n",
    "\n",
    "rawTx = MalardHelpers.getDataFrameFromNetCDF(sourceTandemX)\n",
    "rawSrtm = MalardHelpers.getDataFrameFromNetCDF(sourceSrtm)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(rawTx['lat'].count())\n",
    "print(rawSrtm['lat'].count())\n",
    "\n",
    "print(rawTx['lon'].sum())\n",
    "print(rawSrtm['lon'].sum())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.merge(rawTx,rawSrtm,left_on=['lat','lon'],right_on=['lat','lon'],how='inner')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['lat'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
